//
//  ESWeChatCollectionDAO.m
//  EasyStyle
//
//  Created by FK on 16/5/31.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESWeChatCollectionDAO.h"

@implementation ESWeChatCollectionDAO

+ (instancetype)shareInstance
{
    static ESWeChatCollectionDAO *shareInstance = nil;
    static dispatch_once_t onece;
    dispatch_once(&onece,^{
        shareInstance = [[self alloc]init];
        [shareInstance createEditableCopyOfDatabaseIfNeeded];
    });
    return shareInstance;
}

- (id)init
{
    self = [super init];
    if (self) {
        dbPath = [ESDBBaseDAO applicationDocumentsDirectoryFile:WeChatCollection];
    }
    return self;
}

- (void)createEditableCopyOfDatabaseIfNeeded{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    BOOL plistExits = [fileManager fileExistsAtPath:dbPath];
    if (!plistExits) {
        NSMutableArray *dict = [NSMutableArray array];
        [dict writeToFile:dbPath atomically:YES];
    }
}


- (BOOL)insertCollectionData:(WxNewsObject *)resultItem
{
    if (![self queryExistedData:resultItem]) {
        NSMutableArray *allData = [[NSMutableArray alloc] initWithContentsOfFile:dbPath];
        
        NSDictionary *dict = @{@"newsID":resultItem.newsID,
                               @"title":resultItem.title,
                               @"source":resultItem.source,
                               @"firstImg":resultItem.firstImg,
                               @"mark":resultItem.mark,
                               @"url":resultItem.url};
        
        [allData addObject:dict];
        return [allData writeToFile:dbPath atomically:YES];
    }
    return NO;
}

- (NSMutableArray *)queryAllCollectionData
{
    NSMutableArray *allDict = [[NSMutableArray alloc] initWithContentsOfFile:dbPath];
    
    NSMutableArray *outArr = [NSMutableArray arrayWithCapacity:allDict.count];
    
    for (NSDictionary *dictItem in allDict) {
        
        WxNewsObject *item = [WxNewsObject new];
        item.newsID     = dictItem[@"newsID"];
        item.title      = dictItem[@"title"];
        item.source     = dictItem[@"source"];
        item.firstImg   = dictItem[@"firstImg"];
        item.mark       = dictItem[@"mark"];
        item.url        = dictItem[@"url"];
        
        [outArr addObject:item];
    }
    return outArr;
}

- (BOOL)removeSingleData:(WxNewsObject *)item
{
    if ([self queryExistedData:item]) {
        NSMutableArray *allData = [[NSMutableArray alloc] initWithContentsOfFile:dbPath];

        NSDictionary *dict = @{@"newsID":item.newsID,
                               @"title":item.title,
                               @"source":item.source,
                               @"firstImg":item.firstImg,
                               @"mark":item.mark,
                               @"url":item.url};
        [allData removeObject:dict];
        
        return [allData writeToFile:dbPath atomically:YES];
    }
    
    return NO;
}

- (BOOL)deleteAllData
{
    NSArray *nullArr = [NSArray array];
    return [nullArr writeToFile:dbPath atomically:YES];
}

///   查询数据是否已经在数据库中存在   存在返回：YES
- (BOOL)queryExistedData:(WxNewsObject *)curItem
{
    NSMutableArray *allData = [[NSMutableArray alloc] initWithContentsOfFile:dbPath];
    
    NSDictionary *dict = @{@"newsID":curItem.newsID,
                           @"title":curItem.title,
                           @"source":curItem.source,
                           @"firstImg":curItem.firstImg,
                           @"mark":curItem.mark,
                           @"url":curItem.url};
    return [allData containsObject:dict];
}

@end
